<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Configmodel extends CI_Model {

	protected $_CI;

	public static $searchField = array();
	public static $likeField = array();

	public function __construct(){
		parent::__construct();
		$this->_CI = & get_instance();
	}
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
	public function getConfigList($assist = array()){
		$this->db->from('config');
		if(!empty($assist['where'])) $this->db->where($assist['where']);
		if(!empty($assist['like'])) $this->db->like($assist['like']);
		if(!empty($assist['where_in'])){
			foreach ($assist['where_in'] as $k => $v) {
				if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
				else {
					$query = $this->db->get();
					return array(array(),0);
				}
			}
		}
		$this->db->order_by('config_id','asc');
		$query = $this->db->get();
		$list = $query->result_array();

		return $list;
	}

	public function getConfig($config_name){
		$this->db->from('config');
		$this->db->where('config_name',$config_name);
		$this->db->limit(1);
		$query = $this->db->get();
		$res = $query->row_array();

		$res = id2name('config_value',$res,false);

		return $res;
	}
/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
	public function createConfig($info){
		$this->db->insert('config',$info);
		return $this->db->insert_id();
	}

	public function createConfigHistoryBatch($info){
		if(empty($info)) return true;
		$this->db->insert_batch('config_history',$info);
		return $this->db->insert_id();
	}
/*
| -------------------------------------------------------------------
|  EDIT Functions
| -------------------------------------------------------------------
*/
	public function editConfig($info){
		$this->db->from('config');
		$this->db->where('config_name',$info['config_name']);
		$this->db->limit(1);
		$query = $this->db->get();
		$res = $query->row_array();

		if(!empty($res)){
			$this->db->where('config_name',$info['config_name']);
			$this->db->update('config',$info);
			return $res['config_id'];
		}else{
			$this->db->insert('config',$info);
			return $this->db->insert_id();
		}
	}
/*
| -------------------------------------------------------------------
|  Extern Functions
| -------------------------------------------------------------------
*/
	public function outputConfigList4js($assist = array()){
		$this->db->from('config');
		if(!empty($assist['where'])) $this->db->where($assist['where']);
		if(!empty($assist['like'])) $this->db->like($assist['like']);
		if(!empty($assist['where_in'])){
			foreach ($assist['where_in'] as $k => $v) {
				if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
				else {
					$query = $this->db->get();
					return array(array(),0);
				}
			}
		}
		$query = $this->db->get();
		$list = $query->result_array();

		$result = array();
		foreach ($list as $record) {
			$result[] = array($record['config_name'],$record['config_value']);
		}

		return json_encode($result);
	}
}